#include "includes.h"
#include <string.h> 
#include <stdio.h>  
#include <stdbool.h>
#include <IOdevice.h>
#include "xrj_protocol.h"
#include "Key.h"

//--------------门禁开关----------------------------------------------------------------------------------------------

#define		GPIO_Pin_KEY1			GPIO_Pin_12
#define		GPIO_Pin_KEY2			GPIO_Pin_11

#define		GPIO_Pin_IN1			GPIO_Pin_4
#define		GPIO_Pin_IN2			GPIO_Pin_5
#define		GPIO_Pin_IN3			GPIO_Pin_6
#define		GPIO_Pin_IN4			GPIO_Pin_7

#define		GPIO_Pin_IN6			GPIO_Pin_1
#define		GPIO_Pin_IN7			GPIO_Pin_10

#define		GPIO_Pin_R_LOCK_DET		GPIO_Pin_1
#define		GPIO_Pin_L_LOCK_DET		GPIO_Pin_3



#define 	Read_GPIO_Pin_KEY1			(GPIOA->IDR  & GPIO_Pin_KEY1) 
#define 	Read_GPIO_Pin_KEY2			(GPIOA->IDR  & GPIO_Pin_KEY2)



#define 	Read_GPIO_Pin_IN1      		(GPIOA->IDR  & GPIO_Pin_IN1) 
#define 	Read_GPIO_Pin_IN2      		(GPIOA->IDR  & GPIO_Pin_IN2) 
#define 	Read_GPIO_Pin_IN3      		(GPIOA->IDR  & GPIO_Pin_IN3) 
#define 	Read_GPIO_Pin_IN4      		(GPIOA->IDR  & GPIO_Pin_IN4) 

#define 	Read_GPIO_Pin_IN6      		(GPIOB->IDR  & GPIO_Pin_IN6) 
#define 	Read_GPIO_Pin_IN7      		(GPIOB->IDR  & GPIO_Pin_IN7)


#define 	Read_GPIO_Pin_R_LOCK_DET    (GPIOC->IDR  & GPIO_Pin_R_LOCK_DET) 
#define 	Read_GPIO_Pin_L_LOCK_DET    (GPIOC->IDR  & GPIO_Pin_L_LOCK_DET) 


#define		SW_KEY_KEY1			(1<<0)
#define		SW_KEY_KEY2			(1<<1)


#define		SW_KEY_IN1			(1<<2)
#define		SW_KEY_IN2			(1<<3)
#define		SW_KEY_IN3			(1<<4)
#define		SW_KEY_IN4			(1<<5)
#define		SW_KEY_IN6			(1<<6)
#define 	SW_KEY_IN7			(1<<9)


#define		SW_KEY_R_LOCK_DET 	(1<<7)
#define		SW_KEY_L_LOCK_DET 	(1<<8)



#define		DEBOUNCE_END		0xffff


#define		SW_DEBOUNCE_MAX		4
#define	 	IN_DEBOUNCE_MAX		1
const UINT32 Tab_KeyBit1[2] = {	SW_KEY_KEY1, SW_KEY_KEY2};						//---右按键  左按键

const UINT32 Tab_KeyBit2[8] = {	SW_KEY_IN1, SW_KEY_IN3,							//---右门开  右门关
								SW_KEY_IN2, SW_KEY_IN4,							//---左门开  左门关
								SW_KEY_R_LOCK_DET,SW_KEY_L_LOCK_DET,			//---右门锁  左门锁
								SW_KEY_IN6,SW_KEY_IN7							//---预留									
								};


Deb_BUF st_Deb_KeyBuf = DEB_BUF_INIT;
Deb_BUF st_Deb_InBuf = DEB_BUF_INIT;
								
UINT32 test_Weight = 0;

								
#define D_CNTCHECKMax 50								
UINT32 cntCheck  = D_CNTCHECKMax;		
								


								
void ST_DebBuf_Init(void)
{
	st_Deb_KeyBuf.Max = SW_DEBOUNCE_MAX;
	st_Deb_KeyBuf.TABKeyBit = (UINT32 * )Tab_KeyBit1;
	st_Deb_KeyBuf.TABsize = sizeof(Tab_KeyBit1)/sizeof(UINT32);
	st_Deb_InBuf.Max = IN_DEBOUNCE_MAX;
	st_Deb_InBuf.TABKeyBit = (UINT32 * )Tab_KeyBit2;
	st_Deb_InBuf.TABsize = sizeof(Tab_KeyBit2)/sizeof(UINT32);
	
	

}	

void SWITCH_Key_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure; 
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); //原来的初始化代码没有打开时钟 
	

	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_IN1|GPIO_Pin_IN2|GPIO_Pin_IN3|GPIO_Pin_IN4|GPIO_Pin_KEY1|GPIO_Pin_KEY2; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;     
	GPIO_Init(GPIOA, &GPIO_InitStructure);		
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_IN6|GPIO_Pin_IN7; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;     
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_R_LOCK_DET|GPIO_Pin_L_LOCK_DET; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;     
	GPIO_Init(GPIOC, &GPIO_InitStructure);	
		
	

	ST_DebBuf_Init();	
}

typedef struct _st_MapKeyDevice{
	UINT32 key;
	INT32  index;
}st_MapKeyDevice;

const st_MapKeyDevice  MapKeyDevice_Down[8] = {
	{SW_KEY_KEY1,IOT__UNIT_INDEX__KEY_1},
	{SW_KEY_KEY2,IOT__UNIT_INDEX__KEY_2},
	
	{SW_KEY_IN1,IOT__UNIT_INDEX__SENSOR_1},
	{SW_KEY_IN2,IOT__UNIT_INDEX__SENSOR_2},
	{SW_KEY_IN3,IOT__UNIT_INDEX__PLATE_1},
	{SW_KEY_IN4,IOT__UNIT_INDEX__PLATE_2},
	//---to do
	{SW_KEY_R_LOCK_DET,IOT__UNIT_INDEX__LOCK_1},
	{SW_KEY_L_LOCK_DET,IOT__UNIT_INDEX__LOCK_2},		
};

const st_MapKeyDevice  MapKeyDevice_Up[8] = {
	{SW_KEY_KEY1,IOT__UNIT_INDEX__KEY_1},
	{SW_KEY_KEY2,IOT__UNIT_INDEX__KEY_2},
	{SW_KEY_IN1,IOT__UNIT_INDEX__SENSOR_1},
	{SW_KEY_IN2,IOT__UNIT_INDEX__SENSOR_2},
	
	{SW_KEY_R_LOCK_DET,IOT__UNIT_INDEX__LOCK_1},
	{SW_KEY_L_LOCK_DET,IOT__UNIT_INDEX__LOCK_2},	
};


UINT32 test_cnt = 0;

void DownConduct(UINT32  End, UINT32 KeyBit,Deb_BUF * deb_buf)
{
	INT32 index = -1;
	INT32 i;
	INT32 size;
	
	
	XRJ_mutex_lock(&Device_Value.Lock);	
	switch(End&KeyBit)
	{	
		case SW_KEY_KEY1:
			Device_Value.KEY_1 = IOT__STATUS_CODES__DOWN;
			break;
		case SW_KEY_KEY2:
			Device_Value.KEY_2 = IOT__STATUS_CODES__DOWN;
			break;
		
	
		case SW_KEY_IN1:
			Device_Value.SENSOR_1_Report= IOT__STATUS_CODES__ON;
			break;
		case SW_KEY_IN2:
			Device_Value.SENSOR_2_Report= IOT__STATUS_CODES__ON;
			break;
		case SW_KEY_IN3:
			//Device_Value.PLATE_1_Report = IOT__STATUS_CODES__OFF;
			break;
		case SW_KEY_IN4:
			//Device_Value.PLATE_2_Report = IOT__STATUS_CODES__OFF;
			break;		

		case SW_KEY_R_LOCK_DET:
			Device_Value.LOCK_1_Report= IOT__STATUS_CODES__ON;
			break;
		
		case SW_KEY_L_LOCK_DET:
			Device_Value.LOCK_2_Report= IOT__STATUS_CODES__ON;
			break;		
		
		case SW_KEY_IN6:
			if (test_Weight == 0)
			{
				test_Weight = 0xff;
				Device_Value.WEIGHING_1_Setting = 0xff;
			}
			else
			{
				test_Weight = 0;
				Device_Value.WEIGHING_1_Setting = 0xf0;
			}
				
			break;	
			
		case SW_KEY_IN7:
			if (cntCheck>=D_CNTCHECKMax){
				cntCheck = 0;
				Device_Value.WEIGHING_2_Report++; 
			}
			break;
		default:
			break;
	}	
	XRJ_mutex_unlock(&Device_Value.Lock);

	if ((End&KeyBit) != 0)
	{	
		size = sizeof(MapKeyDevice_Down)/sizeof(st_MapKeyDevice);
		for (i=0; i<size; i++)
		{
			if (MapKeyDevice_Down[i].key == (End&KeyBit)) 
			{
				index = MapKeyDevice_Down[i].index;
			}	
		}
		printf("Down KEY[%d]: %d\n", KeyBit ,(End&KeyBit));
	}
	
	
	if (index >= 0)
	{	
		Send_Report_Message(index,NULL);
	}
}	

void UpConduct(UINT32  End, UINT32 KeyBit, Deb_BUF * deb_buf)
{
	INT32 index = -1;
	INT32 i;
	INT32 size;
	
	XRJ_mutex_lock(&Device_Value.Lock);
	switch(End&KeyBit)
	{
		case SW_KEY_KEY1:
			Device_Value.KEY_1 = IOT__STATUS_CODES__UP;
			break;	
		case SW_KEY_KEY2:
			Device_Value.KEY_2 = IOT__STATUS_CODES__UP;
			break;		
		
		case SW_KEY_IN1:	
			Device_Value.SENSOR_1_Report= IOT__STATUS_CODES__OFF;
			//====如果关闭开关闭合切换成关闭状态
//			if (deb_buf->Old&SW_KEY_IN3){
//				Device_Value.PLATE_1_Report = IOT__STATUS_CODES__OFF;
//				index = IOT__UNIT_INDEX__PLATE_1;
//			}else{
//				Device_Value.PLATE_1_Report = IOT__STATUS_CODES__DOING;
//			}			
			break;		
		case SW_KEY_IN2:
			Device_Value.SENSOR_2_Report= IOT__STATUS_CODES__OFF;
//			if (deb_buf->Old&SW_KEY_IN4){
//				Device_Value.PLATE_2_Report = IOT__STATUS_CODES__OFF;
//				index = IOT__UNIT_INDEX__PLATE_2;
//			}else{
//				Device_Value.PLATE_2_Report = IOT__STATUS_CODES__DOING;
//			}
			break;
		case SW_KEY_IN3:
//			if (deb_buf->Old&SW_KEY_IN1){
//				Device_Value.PLATE_1_Report = IOT__STATUS_CODES__ON;
//				index = IOT__UNIT_INDEX__PLATE_1;
//			}else{
//				Device_Value.PLATE_1_Report = IOT__STATUS_CODES__DOING;
//			}
//			
			break;
		case SW_KEY_IN4:
//			if (deb_buf->Old&SW_KEY_IN2){
//				Device_Value.PLATE_2_Report = IOT__STATUS_CODES__ON;
//				index = IOT__UNIT_INDEX__PLATE_2;
//			}else{
//				Device_Value.PLATE_2_Report = IOT__STATUS_CODES__DOING;
//			}	
			
			break;
			

		case SW_KEY_R_LOCK_DET:
			Device_Value.LOCK_1_Report= IOT__STATUS_CODES__OFF;
			break;
		
		case SW_KEY_L_LOCK_DET:
			Device_Value.LOCK_2_Report= IOT__STATUS_CODES__OFF;
			break;	
		
		case SW_KEY_IN6:
			break;
		
		case SW_KEY_IN7:
			break;	
	}	
	XRJ_mutex_unlock(&Device_Value.Lock);
	
	if ((End&KeyBit) != 0)
	{
		size = sizeof(MapKeyDevice_Up)/sizeof(st_MapKeyDevice);
		for (i=0; i<size; i++)
		{
			if (MapKeyDevice_Up[i].key == (End&KeyBit)) 
			{
				index = MapKeyDevice_Up[i].index;
			}	
		}
		printf("UP   KEY[%d]: %d\n", KeyBit ,(End&KeyBit));
	}	
	
	if (index >= 0)
	{	
		Send_Report_Message(index,NULL);
	}	
}	

void Debouce_Read(Deb_BUF * deb_buf, UINT32 Buf)
{
	deb_buf->In = Buf;
}	

void Debouce_Conduct(Deb_BUF * deb_buf)
{
UINT32 i; 	
UINT32	t_Down = 0;
UINT32	t_Up = 0;	
UINT32  cvt = 0;
UINT32  old = 0;	
	
	if (deb_buf->In == deb_buf->Chk)
	{
		deb_buf->Cnt++;
		if (deb_buf->Cnt >= DEBOUNCE_END)
		{
			deb_buf->Cnt = DEBOUNCE_END;
		}	
		else if (deb_buf->Cnt >= deb_buf->Max)
		{
			deb_buf->Cnt = DEBOUNCE_END;
			deb_buf->Cvt = deb_buf->Chk;
		}	
				
	}
	else
	{
		deb_buf->Chk = deb_buf->In;
		deb_buf->Cnt = 0;
	}	
	

	cvt = deb_buf->Cvt;
	old = deb_buf->Old;
	if (cvt != old)
	{
		t_Down = (old^cvt) & cvt;
		t_Up = (old^cvt) & old;
		deb_buf->Old = deb_buf->Cvt;
		for (i = 0; i < deb_buf->TABsize; i++)
		{
			DownConduct(t_Down,deb_buf->TABKeyBit[i],deb_buf);
			UpConduct(t_Up,deb_buf->TABKeyBit[i],deb_buf);				
		}		
	}	
}	


void KEY_Proc(void)
{
UINT32 	In_Buf = 0;
		
	if (Read_GPIO_Pin_KEY1 == 0)
	{
		In_Buf |= SW_KEY_KEY1;
	}		
	if (Read_GPIO_Pin_KEY2 == 0)
	{
		In_Buf |= SW_KEY_KEY2;
	}	
	
	Debouce_Read(&st_Deb_KeyBuf, In_Buf);
	Debouce_Conduct(&st_Deb_KeyBuf);
}



void IN_Proc(void)
{	
UINT32 	In_Buf = 0;
	cntCheck++;
	if (cntCheck>=D_CNTCHECKMax){
		cntCheck = D_CNTCHECKMax;
	}
	
	if (Read_GPIO_Pin_IN1 == 0)
	{
		In_Buf |= SW_KEY_IN1;
	}	
	
	if (Read_GPIO_Pin_IN2 == 0)
	{
		In_Buf |= SW_KEY_IN2;
	}	
	
	if (Read_GPIO_Pin_IN3 == 0)
	{
		In_Buf |= SW_KEY_IN3;
	}	
	
	if (Read_GPIO_Pin_IN4 == 0)
	{
		In_Buf |= SW_KEY_IN4;
	}	

	if (Read_GPIO_Pin_R_LOCK_DET == 0)
	{
		In_Buf |= SW_KEY_R_LOCK_DET;
	}

	if (Read_GPIO_Pin_L_LOCK_DET == 0)
	{
		In_Buf |= SW_KEY_L_LOCK_DET;
	}		
	
	if (Read_GPIO_Pin_IN6 == 0)
	{
		In_Buf |= SW_KEY_IN6;
	}		

	if (Read_GPIO_Pin_IN7 == 0)
	{
		In_Buf |= SW_KEY_IN7;
	}
	
	Debouce_Read(&st_Deb_InBuf, In_Buf);
	Debouce_Conduct(&st_Deb_InBuf);	
	
}



